package com.jiangjun.fetch.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.jiangjun.fetch.FetchRss;
import com.jiangjun.fetch.Weibo;
import com.jiangjun.fetch.mongo.MongoDBSupport;
import com.jiangjun.fetch.util.UUIDUtil;
import com.mongodb.BasicDBObject;

public class AddWeiboAction extends DispatchAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
//		String id = request.getParameter("id");
		String id = UUIDUtil.getUUID();
		String name = request.getParameter("name");
		name = new String(name.getBytes("iso-8859-1"), "utf-8");

		MongoDBSupport mongoDBSupport = MongoDBSupport.getInstance();
		BasicDBObject param = new BasicDBObject();
		param.put("id", id);
		if (null != mongoDBSupport.queryOneByParam(MongoDBSupport._WEIBO,
				param, null)) {
			return mapping.findForward("success");
		}
		Weibo weibo = new Weibo();
		weibo.setId(id);
		weibo.setName(name);
		weibo.setIsEnable("1");
		mongoDBSupport.save(weibo.toDbObject(), MongoDBSupport._WEIBO);
		return mapping.findForward("success");
	}

}
